package com.tianhui.consignortwofei.mvp.ui.adapter;

import android.content.Context;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.tianhui.consignortwofei.R;
import com.tianhui.consignortwofei.mvp.model.enty.SourcePathInfo;

import org.yczbj.ycrefreshviewlib.adapter.RecyclerArrayAdapter;
import org.yczbj.ycrefreshviewlib.holder.BaseViewHolder;

/**
 * @author fgs
 * @date 2020/8/31
 * @description
 **/
public class SourcePathListAdapter extends RecyclerArrayAdapter<SourcePathInfo> {
    private int selectPosition = 0;

    public SourcePathListAdapter(Context context) {
        super(context);
    }

    public void setSelectPosition(int selectPosition) {
        this.selectPosition = selectPosition;
    }

    @Override
    public BaseViewHolder OnCreateViewHolder(ViewGroup parent, int viewType) {
        return new SourcePathViewHolder(parent, R.layout.item_source_path_list);
    }

    public SourcePathInfo getSelectPathInfo() {
        if (selectPosition < getCount()) {
            return getAllData().get(selectPosition);
        } else {
            return null;
        }
    }

    private class SourcePathViewHolder extends BaseViewHolder<SourcePathInfo> {

        private TextView addressTextView;
        private TextView userTextView;
        private TextView phoneTextView;
        private ImageView imageView;

        public SourcePathViewHolder(ViewGroup parent, int res) {
            super(parent, res);
            addressTextView = getView(R.id.item_source_path_list_addressTextView);
            userTextView = getView(R.id.item_source_path_list_userTextView);
            phoneTextView = getView(R.id.item_source_path_list_phoneTextView);
            imageView = getView(R.id.item_source_path_list_selectImageView);
        }

        @Override
        public void setData(SourcePathInfo data) {
            super.setData(data);
            addressTextView.setText("地址：" + data.placeprovince + data.placecity + data.placecounty + data.placedetails);
            userTextView.setText("联系人：" + data.placeusername);
            phoneTextView.setText("联系电话：" + data.placetel);

            if (selectPosition == getDataPosition()) {
                imageView.setBackgroundResource(R.mipmap.icon_selected);
            } else {
                imageView.setBackgroundResource(R.mipmap.icon_unselected);
            }
        }
    }
}
